# From Final_Code_Replication_Final/02_Analysis/00_Main_Text/Figure_01

# Code for Figure 1: The Effect of Pretrial Incarceration on 2012 Turnout 

rm(list = ls())

## ---------------------------------------
## Load Packages 
## ---------------------------------------

require('dotwhisker')
require('gridExtra')

require('AER')
require('ivpack')
require('dplyr')
require('data.table')

## ---------------------------------------
## Load Data and Functions
## ---------------------------------------
load("../data0812.RData")

directory <- "../functions/"
functions <- list.files(directory)  
loadfunctions <- sapply(functions, FUN = function(x)source(paste0(directory, x)))

## ---------------------------------------
# Construct Instrument
## ---------------------------------------
data0812 <- constructIV(data0812)

## ---------------------------------------
#  Select Last Case Before Election
## ---------------------------------------
data0812 <- lastCase(data0812)

## ---------------------------------------
## Two Stage Least Squares Specifications
## ---------------------------------------
time.controls <- "as.factor(court_time1) + as.factor(court_time2) + as.factor(court_dow) + as.factor(court_shift) + as.factor(totOGS2)"
case.controls <-   "as.factor(any_drug_2) +  as.factor(any_violent_2) + as.factor(fire_arms_2) +  as.factor(any_rob_2) + as.factor(any_dui_2) + as.factor(prior_offender_2)"
demo.controls <- "age_2012 + I(age_2012^2) + Female + as.factor(race) + vote2008 + as.factor(noteli08) + regis_before"

outc.1 <- "vote2012"
endo.1 <- "pti"
inst.1 <- "judgeiv"

form.1 <- formula(paste(outc.1, "~", endo.1, "+" , time.controls, "|", inst.1, "+", time.controls))
form.2 <- formula(paste(outc.1, "~", endo.1, "+" , time.controls, "+", demo.controls, "|", inst.1, "+", time.controls, "+", demo.controls))
form.3 <- formula(paste(outc.1, "~", endo.1, "+" , time.controls, "+", demo.controls, "+", case.controls, "|", inst.1, "+", time.controls, "+", demo.controls, "+", case.controls))

## ---------------------------------------
## Plot Figure 1
## ---------------------------------------
m1a1 <- ivreg(form.1, data = data0812)
m1a2 <- ivreg(form.2, data = data0812)
m1a3 <- ivreg(form.3, data = data0812)

c1 <- c(robust.se(m1a1)["pti", 1], robust.se(m1a1)["pti", 2]) # top line
c2 <- c(robust.se(m1a2)["pti", 1], robust.se(m1a2)["pti", 2]) # middle line
c3 <- c(robust.se(m1a3)["pti", 1], robust.se(m1a3)["pti", 2]) # bottom line

m1 <- data.frame(rbind(c1, c2, c3))
colnames(m1) <- c("estimate", "std.error")
m1$term <- c("col1", "col2", "col3")
m1_df <- data.frame(as.matrix(m1)) 
m1_df$estimate <- as.numeric(as.character(m1_df$estimate))
m1_df$std.error <- as.numeric(as.character(m1_df$std.error))

g0 <- {dwplot(m1_df, 
              dot_args = list(size = 2.5), whisker_args = list(size = 0.75),  
              vline = geom_vline(xintercept = 0, colour = "grey60", linetype = 2))  %>% 
    relabel_predictors(c(col1 = "Fixed effects", col2 = "Fixed effects\n + Demographic covariates", 
                         col3 = "Fixed effects\n + Demographic covariates\n + Case covariates")) +
    theme_classic() + xlab("The Effect of Pretrial Incarceration on Turnout") + ylab("") +
    theme(plot.title = element_text(face="bold"), legend.position = "none",
          axis.text.x  = element_text(size = 12),
          axis.text.y  = element_text(size = 10),
          text = element_text(size = 10))} 

pdf(file = "../output/Figures/Figure_01.pdf", width = 5, height = 3.25)
grid.arrange(g0)
dev.off()

cat("\nFigure 1: The Effect of Pretrial Incarceration on 2012 Turnout\n")
cat("Saved Figure 1 in /output/Figures/Figure_01.pdf\n\n")
